package com.zhc.dejingapi.service.impl;


import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhc.dejingapi.entity.QingEquipmentEntity;
import com.zhc.dejingapi.entity.vo.EquipmentStsVo;
import com.zhc.dejingapi.mapper.QingEquipmentMapper;
import com.zhc.dejingapi.pojo.param.QingDataCommResult;
import com.zhc.dejingapi.pojo.param.QingEquipmentParam;
import com.zhc.dejingapi.service.IQingEquipmentService;
import com.zhc.dejingapi.utils.InitUtil;
import com.zhc.dejingapi.utils.ObjectCopierUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-08-07
 */
@Service
public class QingEquipmentServiceImpl extends ServiceImpl<QingEquipmentMapper, QingEquipmentEntity> implements IQingEquipmentService {

    @Resource
    private InitUtil initUtil;

    @Resource
    private QingEquipmentMapper qingEquipmentMapper;


    @Override
    public QingDataCommResult UpdEquipment(QingEquipmentParam entity) throws IllegalAccessException {
        QingDataCommResult result=new QingDataCommResult();
        QingEquipmentEntity qingEquipmentEntity = new QingEquipmentEntity();
        ObjectCopierUtils.copyProperties(entity,qingEquipmentEntity);
        if(qingEquipmentEntity.getMainId()<1){
            result.setErrCode(-10010);
            result.setErrMsg("设备ID不能为空！");
            return result;
        }

        try{
            qingEquipmentMapper.updateById(qingEquipmentEntity);

        }
        catch (Exception e){
            result.setErrCode(-10016);
            result.setErrMsg("修改失败！\n"+e.getMessage()+"\n"+e.getStackTrace());
        }

        return result;
    }

    @Override
    public QingDataCommResult AddEquipment(QingEquipmentParam entity) throws IllegalAccessException {
        QingDataCommResult result=new QingDataCommResult();
        QingEquipmentEntity qingEquipmentEntity = new QingEquipmentEntity();
        ObjectCopierUtils.copyProperties(entity,qingEquipmentEntity);
        try{
            QueryWrapper<QingEquipmentEntity> queryWrapper=new QueryWrapper<>();

            queryWrapper.eq("qingApplyId",entity.getQingApplyId());
            Long lHaveCount=qingEquipmentMapper.selectCount(queryWrapper);
            if(lHaveCount>0){
                return UpdEquipment(entity);
            }

            queryWrapper=new QueryWrapper<>();

            queryWrapper.eq("eqInnerCode",entity.getEqInnerCode());
            lHaveCount=qingEquipmentMapper.selectCount(queryWrapper);
            if(lHaveCount>0){
                result.setErrCode(-10020);
                result.setErrMsg("设备编码重复！");

            }



            qingEquipmentMapper.insert(qingEquipmentEntity);
            initUtil.addInit(qingEquipmentEntity.getMainId());
            result.setMainId(qingEquipmentEntity.getMainId());
            result.setErrCode(0);
            result.setErrMsg("新增成功！");
        }
        catch (Exception ex){
            result.setErrCode(-10015);
            result.setErrMsg("新增失败！\n"+ex.getMessage()+"\n"+ex.getStackTrace());

        }
        return result;
    }

    @Override
    public QingDataCommResult DelEquipment(QingEquipmentEntity entity) {
        QingDataCommResult result=new QingDataCommResult();
        return result;
    }

    @Override
    public List<List<EquipmentStsVo>> getEquipmentSts() {
        List<EquipmentStsVo> equipmentSts = qingEquipmentMapper.getEquipmentSts();
        List<List<EquipmentStsVo>> lists = ListUtil.split(equipmentSts, 4);
        return lists;
    }

    @Override
    public List<EquipmentStsVo> getEquipmentStsList() {
        List<EquipmentStsVo> equipmentSts = qingEquipmentMapper.getEquipmentSts();
        return equipmentSts;
    }

    @Override
    public List<String> selectEq() {

        return qingEquipmentMapper.selectEq();
    }
}
